package com.newear.marathon.controller;

import com.newear.marathon.annotation.BusinessLogger;
import com.newear.marathon.dto.admin.inquiry.*;
import com.newear.marathon.dto.admin.maintenance.*;
import com.newear.marathon.service.IAdminUserService;
import com.newear.marathon.utils.ListResponseConverter;
import com.newear.marathon.utils.PageModel;
import com.newear.marathon.utils.WebPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController
{
    @Autowired
    private IAdminUserService adminUserService;

    @PostMapping("/inquiry/page")
    public Map userInquiryPage(WebPage page, String username, String loginTime)
    {
        AdminUserInquiryPageRequestDTO requestDTO = new AdminUserInquiryPageRequestDTO();
        //组装分页对象
        PageModel pageModel = new PageModel();
        pageModel.setCurrent(page.getPage());
        pageModel.setSize(page.getLimit());
        pageModel.setField(page.getField());
        pageModel.setOrder(page.getType());
        requestDTO.setPage(pageModel);
        //查询条件
        requestDTO.setUsername(username);
        //日期处理
        if (StringUtils.isNotBlank(loginTime))
        {
            String[] startEndTime = loginTime.split(" - ");
            requestDTO.setStartDateTime(startEndTime[0].trim());
            requestDTO.setEndDateTime(startEndTime[1].trim());
        }
        AdminUserInquiryPageResponseDTO responseDTO = adminUserService.doUserInquiryPage(requestDTO);
        //重组响应对象
        return ListResponseConverter
                .listResponseConverter(responseDTO.getTransactionStatus().isSuccess(), responseDTO.getTransactionStatus().getErrorCode(),
                                       responseDTO.getTransactionStatus().getReplyText(), responseDTO.getPage().getTotal(),
                                       responseDTO.getDataList());
    }

    @PreAuthorize("hasAuthority('user:add')")
    @BusinessLogger(key = "ADMIN", value = "userAddition")
    @PostMapping("/addition")
    public AdminUserAdditionResponseDTO userAddition(@Valid @RequestBody AdminUserAdditionRequestDTO requestDTO)
    {
        AdminUserAdditionResponseDTO responseDTO = adminUserService.doUserAddition(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "userModifyInquiry")
    @PostMapping("/modify/inquiry")
    public AdminUserModifyInquiryResponseDTO userModifyInquiry(@Valid @RequestBody AdminUserModifyInquiryRequestDTO requestDTO)
    {
        AdminUserModifyInquiryResponseDTO responseDTO = adminUserService.doUserModifyInquiry(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('user:edit')")
    @BusinessLogger(key = "ADMIN", value = "userModify")
    @PostMapping("/modify")
    public AdminUserModifyResponseDTO userModify(@Valid @RequestBody AdminUserModifyRequestDTO requestDTO)
    {
        AdminUserModifyResponseDTO responseDTO = adminUserService.doUserModify(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "userBaseInfoModifyInquiry")
    @PostMapping("/base/info/modify/inquiry")
    public AdminUserBaseInfoModifyInquiryResponseDTO userBaseInfoModifyInquiry(
            @Valid @RequestBody AdminUserBaseInfoModifyInquiryRequestDTO requestDTO)
    {
        AdminUserBaseInfoModifyInquiryResponseDTO responseDTO = adminUserService.doUserBaseInfoModifyInquiry(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "userBaseInfoModify")
    @PostMapping("/base/info/modify")
    public AdminUserBaseInfoModifyResponseDTO userBaseInfoModify(@Valid @RequestBody AdminUserBaseInfoModifyRequestDTO requestDTO)
    {
        AdminUserBaseInfoModifyResponseDTO responseDTO = adminUserService.doUserBaseInfoModify(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('user:status')")
    @BusinessLogger(key = "ADMIN", value = "userModifyStatus")
    @PostMapping("/modify/status")
    public AdminUserModifyStatusResponseDTO userModifyStatus(@Valid @RequestBody AdminUserModifyStatusRequestDTO requestDTO)
    {
        AdminUserModifyStatusResponseDTO responseDTO = adminUserService.doUserModifyStatus(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "userModifyPassword")
    @PostMapping("/modify/password")
    public AdminUserModifyPasswordResponseDTO userModifyPassword(@Valid @RequestBody AdminUserModifyPasswordRequestDTO requestDTO)
    {
        AdminUserModifyPasswordResponseDTO responseDTO = adminUserService.doUserModifyPassword(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('user:pwreset')")
    @BusinessLogger(key = "ADMIN", value = "userResetPassword")
    @PostMapping("/reset/password")
    public AdminUserResetPasswordResponseDTO userResetPassword(@Valid @RequestBody AdminUserResetPasswordRequestDTO requestDTO)
    {
        AdminUserResetPasswordResponseDTO responseDTO = adminUserService.doUserResetPassword(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "leftMenuInquiry")
    @PostMapping("/left/menu/inquiry")
    public AdminLeftMenuInquiryResponseDTO leftMenuInquiry(@Valid @RequestBody AdminLeftMenuInquiryRequestDTO requestDTO)
    {
        AdminLeftMenuInquiryResponseDTO responseDTO = adminUserService.doLeftMenuInquiry(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "permissionsInquiry")
    @PostMapping("/permissions/inquiry")
    public AdminPermissionsInquiryResponseDTO permissionsInquiry(@Valid @RequestBody AdminPermissionsInquiryRequestDTO requestDTO)
    {
        AdminPermissionsInquiryResponseDTO responseDTO = adminUserService.doPermissionsInquiry(requestDTO);
        return responseDTO;
    }
}
